package com.html5.weather;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.repackaged.org.json.JSONObject;
import com.html5.utils.Constant;
import com.html5.utils.Utils;

@SuppressWarnings("serial")
public class WeatherServlet extends HttpServlet{
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
		GetWeather getWeather = GetWeather.instance();
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> cwMap = new HashMap<String, String>();
		JSONObject weatherjson=new JSONObject();
		String city = Utils.getBody(request);
		try {
			list = getWeather.getForecastWeather(new JSONObject(city));
			cwMap = getWeather.getCurrentWeather(new JSONObject(city));
			weatherjson.put(Constant.list, list);
			weatherjson.put(Constant.map, cwMap);
			weatherjson.put("hl",new JSONObject(city).getString("hl"));
			byte[] bytes = weatherjson.toString().getBytes(Constant.encode);
		 	response.setContentLength(bytes.length);
		 	response.getOutputStream().write(bytes);
		}catch(Exception e){
			e.printStackTrace(); 
		}
	}
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException{
		 
	}
}
